import {isNotEmpty} from '@/utils/CommonUtils';

const that = {
  mounted(el, binding, vnode, prevVnode) {
    that.bind(el, binding);
  },

  onInput(ele, binding, e) {
    let callback = binding.value;
    if (e.key === 'Enter') {
      if (isNotEmpty(callback)) {
        callback();
      }
    }
  },

  updated(el, binding, vnode, prevVnode) {
    that.bind(el, binding);
  },

  beforeUnmount(el, binding, vnode, prevVnode) {
    try {
      $(ele).unbind(that.onInput);
    } catch (e) {
    }
  },


  bind(ele, binding) {
    $(ele).unbind(that.onInput).bind('keydown',
      (e) => that.onInput(ele, binding, e));
  },
};

export default that;